<?php

class ovfFileCsv {
	public $sFilePath="";
	public $sMode='r';
	public $bHeader=false;
	public $sDelim=";";
	public $sEnclos='"';
	public $desEsito='OK';
	public $codEsito='200';
	
	private $fh=null;
	private $aCampi=array();
	private $bHeaderWritten=false;

   public function __construct($sFilePath='', $sMode='r', $bHeader=false, $sDelim=';',$sEnclos='"') {
   	if ($sFilePath != '') {
   		return $this->open($sFilePath, $sMode, $bHeader, $sDelim, $sEnclos);
   	}
   }
   
   public function open($sFilePath='', $sMode='r', $bHeader=false, $sDelim=';',$sEnclos='"') {
   	$this->fh = fopen($sFilePath,$sMode.'b');
   	$this->bHeaderWritten = false;
   	if (!$this->fh) {
   		$this->desEsito = sprintf("errore open '%s' - modalita' '$s'",$sFilePath,$sMode);
   		$this->codEsito = '501';
   		return false;
   	} 
   	$this->sFilePath = $sFilePath;
   	$this->sMode = $sMode;
   	$this->bHeader = $bHeader;
   	$this->sDelim = $sDelim;
   	$this->sEnclos = $sEnclos;
   	$this->aCampi = array();
   }

   public function read() {
   	if (!$this->fh) {
   		$this->desEsito = sprintf("File '%s' - modalita' '$s' - non aperto",$this->sFilePath,$this->sMode);
   		$this->codEsito = '501';
   		return false;
   	}
   	if ($this->sMode != 'r') {
   		$this->desEsito = sprintf("File '%s' - modalita' '$s' - non aperto in lettura",$this->sFilePath,$this->sMode);
   		$this->codEsito = '502';
   		return false;
   	}
   	if ($this->bHeader) return $this->readH();
   	return fgetcsv($this->fh,4048,$this->sDelim,$this->sEnclos);
   }
   
   public function write($aRec) {
   	if (!$this->fh) {
   		$this->desEsito = sprintf("File '%s' - modalita' '$s' - non aperto",$this->sFilePath,$this->sMode);
   		$this->codEsito = '503';
   	}
   	if ($this->sMode != 'w') {
   		$this->desEsito = sprintf("File '%s' - modalita' '$s' - non aperto in scrittura",$this->sFilePath,$this->sMode);
   		$this->codEsito = '504';
   	}
   	if ($this->bHeader and !$this->bHeaderWritten) {
   		$this->aCampi = array();
   		foreach($aRec as $nX => $vX) {
   			$this->aCampi[$nX] = "";
   		}
   		fputcsv($this->fh,array_keys($this->aCampi),$this->sDelim,$this->sEnclos);
   		$this->bHeaderWritten = true;
   	}
   	$aVal = array();
   	foreach($this->aCampi as $nX => $vX) {
   		$aVal[] = (isset($aRec[$nX]))?$aRec[$nX]:"";
    	}
   	return fputcsv($this->fh,$aVal,$this->sDelim,$this->sEnclos);
   }
   
   public function close() {
   	if (!$this->fh) {
   		$this->desEsito = sprintf("File '%s' - modalita' '$s' - non aperto",$this->sFilePath,$this->sMode);
   		$this->codEsito = '505';
   		return false;
   	}
   	fclose($this->fh);
   	return true;
   }
   
   private function readH() {
	  	if (count($this->aCampi) == 0) {
   		$aCampi = fgetcsv($this->fh,4048,$this->sDelim,$this->sEnclos);
   		if (!$aCampi) {
	   		return false;
   		}
   		$this->aCampi = array();
   		foreach($aCampi as $kk => $nome) {
   			$nome = trim($nome);
   			if ($nome == "") $nome = "x";
	   		$nn = 2;
	   		$suff = "";
   		 	while(isset($this->aCampi[$nome.$suff])) {
   		 		$suff = $nn;
   		 		$nn++;
   		 	}
   		 	$this->aCampi[$nome.$suff] = "";
   		}
	  	}
   	$aVal = fgetcsv($this->fh,4048,$this->sDelim,$this->sEnclos);
   	if (!$aVal) return false;
   	$aRet = $this->aCampi;
   	$kk=0;
   	foreach($aRet as $nX => $vX) {
   		if (!isset($aVal[$kk])) break;
   		$aRet[$nX] = $aVal[$kk];
   		$kk++;
     	}
  		return $aRet;
   }
}
?>